package cc.comac.saic.hkhy.jsrj.gj.inttools.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cc.comac.saic.hkhy.jsrj.gj.inttools.async.AsyncTaskStatus;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cc.comac.saic.hkhy.jsrj.gj.inttools.mapper.SysAsyncTaskMapper;
import cc.comac.saic.hkhy.jsrj.gj.inttools.pojo.entity.SysAsyncTask;
import cc.comac.saic.hkhy.jsrj.gj.inttools.service.SysAsyncTaskService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 异步任务表(SysAsyncTask)表服务实现类
 *
 * @author makejava
 * @since 2025-11-20 10:29:15
 */
@Service
public class SysAsyncTaskServiceImpl extends ServiceImpl<SysAsyncTaskMapper, SysAsyncTask> implements SysAsyncTaskService {

    @Autowired
    private SysAsyncTaskMapper asyncTaskMapper;

    @Override
    @Transactional
    public int createTask(SysAsyncTask task) {
        task.setStatus(AsyncTaskStatus.PENDING.getCode());
        task.setProgress(0);
        return asyncTaskMapper.insertTask(task);
    }

    @Override
    public SysAsyncTask getTaskById(Long taskId) {
        return asyncTaskMapper.selectTaskById(taskId);
    }

    @Override
    public List<SysAsyncTask> selectTaskList(SysAsyncTask task) {
        return asyncTaskMapper.selectTaskList(task);
    }

    @Override
    @Transactional
    public int updateTask(SysAsyncTask task) {
        return asyncTaskMapper.updateTask(task);
    }

    @Override
    @Transactional
    public int updateTaskProgress(Long taskId, Integer progress) {
        return asyncTaskMapper.updateTaskProgress(taskId, progress);
    }

    @Override
    @Transactional
    public int updateTaskStatus(Long taskId, String status, String errorMessage) {
        SysAsyncTask task = new SysAsyncTask();
        task.setId(taskId);
        task.setStatus(status);

        if (AsyncTaskStatus.RUNNING.getCode().equals(status)) {
            task.setStartTime(new Date());
        } else if (AsyncTaskStatus.SUCCESS.getCode().equals(status) ||
            AsyncTaskStatus.FAILED.getCode().equals(status) ||
            AsyncTaskStatus.CANCELLED.getCode().equals(status)) {
            task.setEndTime(new Date());
        }

        if (errorMessage != null) {
            task.setErrorMessage(errorMessage);
        }

        return asyncTaskMapper.updateTask(task);
    }

    @Override
    @Transactional
    public int updateTaskResult(Long taskId, String resultData, String status) {
        return asyncTaskMapper.updateTaskResult(taskId, resultData, status);
    }

    @Override
    @Transactional
    public int deleteTaskByIds(Long[] taskIds) {
        return asyncTaskMapper.deleteTaskByIds(taskIds);
    }

    @Override
    @Transactional
    public void cleanExpiredTasks(int days) {
        List<SysAsyncTask> expiredTasks = asyncTaskMapper.selectExpiredTasks(days);
        if (!expiredTasks.isEmpty()) {
            Long[] taskIds = expiredTasks.stream().map(SysAsyncTask::getId).toArray(Long[]::new);
            asyncTaskMapper.deleteTaskByIds(taskIds);
        }
    }

    @Override
    public Map<String, Object> getTaskStats() {
        // 这里可以统计各种状态的任务数量
        Map<String, Object> stats = new HashMap<>();
        // 实现统计逻辑...
        return stats;
    }

}

